<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .one {
            width: 100%;
            height: 200px;
            background-color: red;
        }

        .two {
            width: 100%;
            height: 200px;
            background-color: blue;
        }
    </style>
</head>

<body>
    <div id="root">

    </div>
</body>
<script src="../react.js"></script>
<script src="../react-dom.js"></script>
<!-- 用来将jsx语法转化为js语法 -->
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.3/jquery.js"></script>
<script type="text/babel">
    console.log(React)
    // 创建上下文对象 (服务商-中国联通)
    let Context = React.createContext();
    console.log(Context)
    class App extends React.Component {
        state = {
            age: 19
        }
        render() {
            return (
                <div>
                    {
                        // value属性表示传递的值
                    }
                    <Context.Provider value={{
                        name: "张三", age: this.state.age, setAge: (newAge) => {
                            this.setState({
                                age: newAge
                            })
                        }
                    }}>
                        <One />
                        <Two />
                    </Context.Provider>
                </div>
            )
        }
    }
    class One extends React.Component {
        state = {

        }
        render() {
        
            return (
                <Context.Consumer>
                    {
                        (value) => {
                            console.log(value)
                            return (
                                <div className='one'>one组件--{value.name}---{value.age}</div>
                            )
                        }
                    }
                </Context.Consumer>
            )
        }
    }
    class Two extends React.Component {
        state = {

        }
        render() {
            return (
                <Context.Consumer>
                    {
                        (value) => {
                            console.log(value)
                            return (
                                <div className='two'>two组件---{value.name}---{value.age}

                                    <button onClick={() => {
                                        value.setAge(28)
                                    }}>改变年龄</button>
                                </div>
                            )
                        }
                    }
                </Context.Consumer>
            )
        }
    }
    ReactDOM.render(<App />, document.getElementById("root"))
</script>

</html>